package com.zjt.web.api;

import com.zjt.service.product.ICodeService;
import com.zjt.service.user.IClientUserService;
import com.zjt.web.api.vo.base.RecordVO;
import com.zjt.web.api.vo.base.StateVO;
import com.zjt.web.api.vo.request.QRCodeValidateReqVO;
import com.zjt.web.api.vo.response.QRCodeValidateRespVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-14
 * Time: AM10:51
 * 二维码请求接口 , 包括：
 * 1. 二维码验证结果
 */

@Controller
@RequestMapping(value = "/api/qrcode")
public class QRCodeAPIController {
    private static Logger logger = LoggerFactory.getLogger(QRCodeAPIController.class);

    private IClientUserService clientUserService;
    private ICodeService codeService;
    @Autowired
    public void setClientUserService(IClientUserService clientUserService) {
        this.clientUserService = clientUserService;
    }
    @Autowired
    public void setCodeService(ICodeService codeService) {
        this.codeService = codeService;
    }
    /**
     * 二维码验证
     *
     * @param qrCodeValidateReqVO
     * @return
     */
    @RequestMapping(value = {"validate.json", ""})
    @ResponseBody
    public QRCodeValidateRespVO validate(QRCodeValidateReqVO qrCodeValidateReqVO) {
        logger.info("/api/qrcode/validate request parms = " + qrCodeValidateReqVO.toString());
        //TODO 进行请求参数的解析
        StateVO stateVO = new StateVO();
        RecordVO recordVO = null;
        try
        {
            if (this.clientUserService.checkExistByUserId(qrCodeValidateReqVO.getUserId()))
            {
                recordVO = this.codeService.validateByQrCode(qrCodeValidateReqVO.getQrcode(),qrCodeValidateReqVO.getUserId());
                stateVO.setMsg("OK");
                stateVO.setCode(0);
            }else
            {
                stateVO.setCode(-1);
                stateVO.setMsg("client userId 不存在");
                logger.error("client userId 不存在");
            }
        }catch (Exception e){}

        QRCodeValidateRespVO qrCodeValidateRespVO = new QRCodeValidateRespVO();
        //TODO 进行业务逻辑的处理，封装应答数据
        qrCodeValidateRespVO.setActivityVO(null);
        qrCodeValidateRespVO.setRecordVO(recordVO);
        qrCodeValidateRespVO.setStateVO(stateVO);
        logger.info("/api/qrcode/validate response result = " + qrCodeValidateRespVO.toString());
        return qrCodeValidateRespVO;
    }
}
